# -*- coding: utf-8 -*-

# Copyright (c) 2011, Sergey Karakovskiy, Nikolay Sohryakov
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of Yasker nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
from PyQt4 import QtCore
import os
import shutil
import sys


__author__ = 'Sergey Karakovskiy, Nikolay Sohryakov'
__copyright__ = 'Copyright 2011, Yasker'
__license__ = 'New BSD'
__email__ = 'Nikolay.Sohryakov+yasker@gmail.com, Sergey.Karakovskiy+yasker@gmail.com'

APPNAME = 'Yasker'


def getAppDataPath():
	"""
	Returns path to the application data folder depending on the running OS.
	"""
	if sys.platform == 'darwin':
		from AppKit import NSSearchPathForDirectoriesInDomains
		# http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSSearchPathForDirectoriesInDomains
		# NSApplicationSupportDirectory = 14
		# NSUserDomainMask = 1
		# True for expanding the tilde into a fully qualified path
		appdata = os.path.join(
			NSSearchPathForDirectoriesInDomains(14, 1, True)[0], APPNAME)
	elif sys.platform == 'win32':
		appdata = os.path.join(os.environ['APPDATA'], APPNAME)
	else:
		appdata = os.path.expanduser(os.path.join("~", "." + APPNAME))
	return appdata


def getDatabasePath(user):
	"""
	Get a fully qualified path to the storage.db file for the given user.
	If file does not exist, then a new one is created and a path to this file
	is returned.
	"""
	db_path = os.path.join(getAppDataPath(), user.lower())
	if not os.path.isfile(os.path.join(db_path, 'storage.db')):
		db_resource = os.path.join(os.getcwd(), 'resources', 'storage', 'storage.db')
		os.makedirs(db_path)
		shutil.copy(db_resource, db_path)

	return os.path.join(db_path, 'storage.db')


def saveCredentials(jid, pwd, is_auto_login):
#	settings_path = getAppDataPath()
#	settings = QtCore.QSettings(settings_path, QtCore.QSettings.IniFormat)
	settings = QtCore.QSettings(QtCore.QSettings.IniFormat,
	                            QtCore.QSettings.UserScope, 'Yasker', 'Yasker')
	settings.setValue('jid', QtCore.QVariant(jid))
	settings.setValue('pwd', QtCore.QVariant(pwd))
	settings.setValue('is_auto_login', QtCore.QVariant(is_auto_login))


def getCredentials():
	settings = QtCore.QSettings(QtCore.QSettings.IniFormat,
	                            QtCore.QSettings.UserScope, 'Yasker', 'Yasker')
	jid = settings.value('jid', '').toString()
	pwd = settings.value('pwd', '').toString()
	is_auto_login = settings.value('is_auto_login').toBool()
	pwd = pwd.replace('\\', '\\\\') # replace \ with \\
	return jid, pwd, is_auto_login
